package my.Interceptor.common;

import my.Interceptor.common.annotation.Work;
import my.Interceptor.common.enums.WorkType;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class AspectWork {
	private Logger log = Logger.getLogger(AspectWork.class);
	/**
	 * 切入点
	 */
	@Pointcut("@within(ai.fitme.te.interceptor.annotation.Work)")
    private void adviceWork(){}
    
    /**
     * 
     * @param point
     * @return
     */
    @Around(value="adviceWork() && args(json,api_key,timestamp,version,sign)")
    public Object aroundMethod(ProceedingJoinPoint point,String json,String api_key,Long timestamp,String version,String sign){
        Object result = null;
        String methodName = point.getSignature().getName();
        try {
        	log.debug("进入切点");
        	//前置处理
        	Work work = point.getTarget().getClass().getAnnotation(Work.class);
            WorkType[] values = work.value();
            if(hasWork(values, WorkType.LOG)){
            	log.debug("【"+methodName+"输入】"+json);
            	log.debug("【"+methodName+"验证】"+api_key+"\t"+timestamp+"\t"+version+"\t"+sign);
            }
            if(hasWork(values, WorkType.SERECT)){
            	
            }
            if(hasWork(values, WorkType.TOKEN)){
            	
            }
            result = point.proceed();
            if(hasWork(values, WorkType.LOG)){
            	log.debug("【"+methodName+"结果】"+result);
            }
        } catch (Throwable e) {
        	log.error(e.getMessage(),e);
            throw new RuntimeException(e);
        }
        return result;
    }
    
    private boolean hasWork(WorkType[] values,WorkType workType){
    	for (WorkType value : values) {
			if(value==workType)
				return true;
		}
    	return false;
    }
}
